/*
 * date         Author     Note
 * 2015.04.15   wzwz       单系统摇床压缩行的bug
 * 2015.04.16   wzwz       双系统摇床压缩
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mh/common.h>
#include <mh/com.h>
#include <mh/sram.h>
#include "knit.h"
#include "prefixknit.h"
#include "backpiece.h"
#include "translator.h"
#include "compress.h"

/*
 * @note: 判断是否空行
 */
static int compress_is_null_row(KNIT_PAGE *pcon)
{
    if ((pcon->act_1FA == 0) &&
        (pcon->act_1BA == 0) &&
        (pcon->code_1FA == 0) &&
        (pcon->code_1FH == 0) &&
        (pcon->code_1BA == 0) &&
        (pcon->code_1BH == 0))
    {
        return TRUE;
    }
    return FALSE;
}
/*
 * @translator:转换器
 * @program_info: 控制信息
 * @src_row:原始行(1开始)
 * @des_row:目的行(1开始)
 * @total_rows:总行数
 * @threhold[out]:阈值
 * @lspeed[out]:速度限制
 * @ret:0,不能压缩;1，可以压缩；2，可以压缩，需要交换当前行与下一行的执行顺序
 */
int can_compress_ss(MH_KNIT *p,
                    KNIT_PAGE *pcon,
                    int src_row,
                    int des_row,
                    int total_rows,
                    short *threhold,
                    short *lspeed)
{
    int mactype = (int)(p->inch_type + 0.5);
    int safe_len = 0;

    int start[2] = { 0 };
    int end[2] = { 0 };
    int i = 0;
    int prow[2] = { 0 };
    int pcolor[2] = { 0 };
    int cur_rock_num = 0;
    int nxt_rock_num = 0;

    if (src_row + 1 > total_rows)
    {
        return FALSE;
    }
    //编织不能合并
    for (i = 0; i < 2; i++)
    {
        if (pcon[i].gauze1_1 || pcon[i].gauze1_9 ||
            pcon[i].gauze2_1 || pcon[i].gauze2_9)
        {
            return FALSE;
        }
    }

    //非翻针不能合并
    for (i = 0; i < 2; i++)
    {
        if (!(IS_TRANSFER (pcon[i].act_1BA) ||
            IS_TRANSFER (pcon[i].act_1FA) ||
            IS_TRANSFER (pcon[i].act_2BA) ||
            IS_TRANSFER (pcon[i].act_2FA)))
        {
            return FALSE;
        }
    }

    cur_rock_num = pcon[0].rock.needle;
    nxt_rock_num = pcon[1].rock.needle;

    if ((cur_rock_num >= 4) || (nxt_rock_num >= 4))
    {
        return FALSE;
    }

    /*节约冲突不能合并*/
    if(pcon[0].end.forbid || pcon[1].end.forbid)
    {
        printf("conflict1 economy for compress\n");
        return FALSE;
    }
    if(pcon[0].end.ee)
    {
        printf("conflict2 economy for compress\n");
        return FALSE;
    }
    if(pcon[1].end.es)
    {
        printf("conflict3 economy for compress\n");
        return FALSE;
    }

    safe_len = p->select2_to_mac_left_len - p->select1_to_mac_left_len;
    if(safe_len < 0)
    {
        safe_len = -safe_len;
    }
    safe_len = safe_len * p->inch_type / MM_PER_INCH;

    safe_len += 6 * mactype;

    //读取当前行色码范围
    for (i = 0; i < 2; i++)
    {
        prow[0] = pcon[i].flw_1F;
        prow[1] = pcon[i].flw_1B;
        pcolor[0] = pcon[i].code_1FA | pcon[i].code_1FH;
        pcolor[1] = pcon[i].code_1BA | pcon[i].code_1BH;
        translator_get_color_range(p->flower_data, p->flower_width,
                p->flower_height, prow, pcolor, &start[i], &end[i]);

        if (start[i] < 0)
        {
            return FALSE;
        }
    }


    if(des_row & 1)
    {
        if (start[1] - end[0] > safe_len)
        {
            *threhold = end[0];

            *lspeed = 90;
            return 1;
        }
        else if(start[0] - end[1] > safe_len)
        {
            /* 交换当前行与下一行 */
            *threhold = end[1];
            *lspeed = 90;
            return 2;
        }
    }
    else
    {
        if (start[0] - end[1] > safe_len)
        {
            *threhold = start[0];

            *lspeed = 90;
            return 1;
        }
        else if(start[1] - end[0] > safe_len)
        {
            /* 交换当前行与下一行 */
            *threhold = start[1];

            *lspeed = 90;
            return 2;
        }
    }
    return FALSE;
}
/*
 * @brief:通过系统1和系统2的起点和终点，确定整行的起点和终点
 * @start:整行的起点
 * @end:整行的终点
 * @ss:两个系统的起点
 * @se:两个系统的终点
 */
static void get_range (int *start, int *end, int *ss, int *se)
{
    *start = -1;
    if ((ss[0] >= 0) && (ss[1] >= 0))
    {
        *start = __min (ss[0], ss[1]);
    }
    else if (ss[0] >= 0)
    {
        *start = ss[0];
    }
    else if (ss[1] >= 0)
    {
        *start = ss[1];
    }

    *end = -1;
    if ((se[0] >= 0) && (se[1] >= 0))
    {
        *end = __max (se[0], se[1]);
    }
    else if (se[0] >= 0)
    {
        *end = se[0];
    }
    else if (se[1] >= 0)
    {
        *end = se[1];
    }
}
/*
 * @translator:转换器
 * @program_info: 控制信息
 * @row:当前行
 * @total_rows:总行数
 * @threhold[out]:阈值
 * @lspeed[out]:速度限制
 * @ret:0,can not compress;not 0,can compress, 1, 不需要交换，2需要交换当前行和下一行
 */
int can_compress(MH_KNIT *p,
                 KNIT_PAGE *pcon,
                 int src_row,
                 int des_row,
                 int total_rows,
                 short *threhold,
                 short *lspeed)
{
    int rocktype = p->rock_type;
    int mactype = (int)(p->inch_type + 0.5);
    int safe_len = 0;

    int start[2] = { 0 };
    int end[2] = { 0 };
    int i = 0;
    int s[2] = { 0 };
    int e[2] = { 0 };
    int prow[2] = { 0 };
    int pcolor[2] = { 0 };
    int cur_rock_num = 0;
    int nxt_rock_num = 0;

    if (src_row + 3 > total_rows)
    {
        return FALSE;
    }

    //编织不能合并
    for (i = 0; i < 3; i++)
    {
        if (pcon[i].gauze1_1 || pcon[i].gauze1_9 ||
            pcon[i].gauze2_1 || pcon[i].gauze2_9)
        {
            return FALSE;
        }
    }

    //非翻针不能合并
    for (i = 0; i < 2; i++)
    {
        if (!(IS_TRANSFER (pcon[i].act_1BA) ||
            IS_TRANSFER (pcon[i].act_1FA) ||
            IS_TRANSFER (pcon[i].act_2BA) ||
            IS_TRANSFER (pcon[i].act_2FA)))
        {
            return FALSE;
        }
    }

    //第三行不是空行不能合并
    if (!(IS_NONE_ACT (pcon[2].act_1BA) &&
            IS_NONE_ACT (pcon[2].act_1FA) &&
            IS_NONE_ACT (pcon[2].act_2BA) &&
            IS_NONE_ACT (pcon[2].act_2FA)))
    {
        return FALSE;
    }

    //第4行系统1必须是单面
    pcolor[0] = pcon[3].code_1FA | pcon[3].code_1FH;
    pcolor[1] = pcon[3].code_1BA | pcon[3].code_1BH;

    if (rocktype == ROCK_BACK)       //后板摇床
    {
        if (!((pcolor[1] & 0x7FFF) == 0x0000))
        {
            return FALSE;
        }
    }
    else if (rocktype == ROCK_FRONT)  //前板摇床
    {
        if (!((pcolor[0] & 0x7FFF) == 0x0000))
        {
            return FALSE;
        }
    }
    else
    {
        return FALSE;
    }

    cur_rock_num = pcon[0].rock.needle;
    nxt_rock_num = pcon[1].rock.needle;

    if ((cur_rock_num >= 4) || (nxt_rock_num >= 4))
    {
        return FALSE;
    }

    /*节约冲突不能合并*/
    if(pcon[0].end.forbid || pcon[1].end.forbid)
    {
        printf("conflict1 economy for compress\n");
        return FALSE;
    }
    if(pcon[0].end.ee)
    {
        printf("conflict2 economy for compress\n");
        return FALSE;
    }
    if(pcon[1].end.es)
    {
        printf("conflict3 economy for compress\n");
        return FALSE;
    }

    safe_len = p->select4_to_mac_left_len - p->select1_to_mac_left_len;
    if(safe_len < 0)
    {
        safe_len = -safe_len;
    }
    safe_len = safe_len * p->inch_type / MM_PER_INCH;

    safe_len += 5 * mactype;


    if ((cur_rock_num >= 3) || (nxt_rock_num >= 3))
    {
        safe_len += mactype;
    }

    //读取当前行色码范围
    for (i = 0; i < 2; i++)
    {
        prow[0] = pcon[i].flw_1F;
        prow[1] = pcon[i].flw_1B;
        pcolor[0] = pcon[i].code_1FA | pcon[i].code_1FH;
        pcolor[1] = pcon[i].code_1BA | pcon[i].code_1BH;
        translator_get_color_range(p->flower_data, p->flower_width,
                p->flower_height, prow, pcolor, &s[0], &e[0]);

        prow[0] = pcon[i].flw_2F;
        prow[1] = pcon[i].flw_2B;
        pcolor[0] = pcon[i].code_2FA | pcon[i].code_2FH;
        pcolor[1] = pcon[i].code_2BA | pcon[i].code_2BH;
        translator_get_color_range(p->flower_data, p->flower_width,
                p->flower_height, prow, pcolor, &s[1], &e[1]);

        get_range (&start[i], &end[i], s, e);
    }

    if (start[0] < 0 || start[1] < 0)
    {
        return FALSE;
    }

    if (src_row & 1)
    {
        if (start[1] - end[0] > safe_len)
        {
            *threhold = end[0];
            *lspeed = 90;
            return 1;       //不需要交换
        }
        else if (start[0] - end[1] > safe_len)
        {
            *threhold = end[1];
            *lspeed = 90;
            return 2;       //需要交换当前行和下一行的执行顺序
        }
    }
    else
    {
        if (start[0] - end[1] > safe_len)
        {
            *threhold = start[0];
            *lspeed = 90;
            return 1;       //不需要交换
        }
        else if (start[1] - end[0] > safe_len)
        {
            *threhold = start[1];
            *lspeed = 90;
            return 2;       //需要交换当前行和下一行的执行顺序
        }
    }
    return FALSE;
}
/*
 * @translator:转换器
 * @program_info: 控制信息
 * @row:当前行
 * @total_rows:总行数
 * @lspeed[out]:速度限制
 * @range0[out]:第一行范围
 * @range2[out]:第三行范围
 * @start[out]:起点，start[0]左边色码段的起点，start[1]右边色码段的起点
 * @end[out]:终点，end[0]左边色码段的终点，end[1]右边色码段的终点
 * @ret:0,can not compress;not 0,can compress
 */
int can_compress2 (MH_KNIT *p,
                   KNIT_PAGE *pcon,
                   int row,
                   int total_rows,
                   short *lspeed,
                   int *range0,
                   int *range2,
                   int *start,
                   int *end)
{
    int oneside = FALSE;
    int rocktype = p->rock_type;
    int mactype = (int)(p->inch_type + 0.5);
    int safe_len = 0;

    int s1[GAUZE_NUM] = { 0 };
    int e1[GAUZE_NUM] = { 0 };
    int i = 0;
    int n = 0;
    int prow[2] = { 0 };
    int pcolor[2] = { 0 };
    //int rock3 = FALSE;
    int s0, s2;
    int e0, e2;
    int colormask = 0;
    int s4, e4;
    int rocknum = 0;

    if (rocktype > 1)           //只支持前板摇床或者后板摇床
    {
        return FALSE;
    }

    for (i = 0; i < GAUZE_NUM; i++)
    {
        s1[i] = -1;
        e1[i] = -1;
    }

    if (row + 4 > total_rows)
    {
        return FALSE;
    }

    //编织不能合并
    for (i = 0; i < 4; i++)
    {
        if (pcon[i].gauze1_1 || pcon[i].gauze1_9 ||
            pcon[i].gauze2_1 || pcon[i].gauze2_9)
        {
            return FALSE;
        }
    }

    //非翻针不能合并
    for (i = 0; i < 3; i++)
    {
        if (!(IS_TRANSFER (pcon[i].act_1BA) ||
            IS_TRANSFER (pcon[i].act_1FA) ||
            IS_TRANSFER (pcon[i].act_2BA) ||
            IS_TRANSFER (pcon[i].act_2FA)))
        {
            return FALSE;
        }
    }

    //第4行不是空行不能合并
    if (!(IS_NONE_ACT (pcon[3].act_1BA) &&
            IS_NONE_ACT (pcon[3].act_1FA) &&
            IS_NONE_ACT (pcon[3].act_2BA) &&
            IS_NONE_ACT (pcon[3].act_2FA)))
    {
        return FALSE;
    }

    //第5行系统1必须是单面
    pcolor[0] = (pcon[4].code_1FA | pcon[4].code_1FH) & 0x7FFF;
    pcolor[1] = (pcon[4].code_1BA | pcon[4].code_1BH) & 0x7FFF;

    if ((pcolor[0] != 0) && (pcolor[1] != 0))
    {
        return FALSE;
    }

    prow[0] = pcon[4].flw_1F;
    prow[1] = pcon[4].flw_1B;

    translator_get_color_range(p->flower_data, p->flower_width,
            p->flower_height, prow, pcolor, &s4, &e4);
    oneside = translator_is_one_side (p->flower_data, p->flower_width,
            p->flower_height, prow, pcolor);

    rocknum = __max (pcon[0].rock.needle, pcon[1].rock.needle);

    if (rocknum >= 5)
    {
        return FALSE;
    }

    safe_len = p->select4_to_mac_left_len - p->select1_to_mac_left_len;
    if(safe_len < 0)
    {
        safe_len = -safe_len;
    }
    safe_len = safe_len * p->inch_type / MM_PER_INCH;

    safe_len += 6 * mactype;

    //获取第一行的起点和终点
    prow[0] = pcon[0].flw_1F;
    prow[1] = pcon[0].flw_1B;
    pcolor[0] = pcon[0].code_1FA | pcon[0].code_1FH;
    pcolor[1] = pcon[0].code_1BA | pcon[0].code_1BH;
    translator_get_color_range(p->flower_data, p->flower_width,
                p->flower_height, prow, pcolor, &s0, &e0);
    //获取第三行的起点和终点
    prow[0] = pcon[2].flw_1F;
    prow[1] = pcon[2].flw_1B;
    pcolor[0] = pcon[2].code_1FA | pcon[2].code_1FH;
    pcolor[1] = pcon[2].code_1BA | pcon[2].code_1BH;
    translator_get_color_range(p->flower_data, p->flower_width,
                p->flower_height, prow, pcolor, &s2, &e2);
    //下一行的色码范围
    prow[0] = pcon[1].flw_1F;
    prow[1] = pcon[1].flw_1B;
    pcolor[0] = pcon[1].code_1FA | pcon[1].code_1FH;
    pcolor[1] = pcon[1].code_1BA | pcon[1].code_1BH;
    colormask = pcolor[0] | pcolor[1];
    pcolor[0] = pcon[1].code_2FA | pcon[1].code_2FH;
    pcolor[1] = pcon[1].code_2BA | pcon[1].code_2BH;
    colormask |= pcolor[0] | pcolor[1];
    n = translator_get_yinta_range(p->flower_data, p->flower_width,
            p->flower_height, prow, s1, e1, &colormask);


    pcolor[0] = pcon[4].code_1FA | pcon[4].code_1FH;
    pcolor[1] = pcon[4].code_1BA | pcon[4].code_1BH;

    if (rocktype == ROCK_BACK) //后板摇床
    {
        if (pcolor[1] & 0x7FFF)
        {
            if (!oneside)
            {
                return FALSE;
            }

            if ((e1[0] - s4) < rocknum ||
                (e4 - s1[1]) < rocknum)
            {
                return FALSE;
            }
        }
    }
    else if (rocktype == ROCK_FRONT) //前板摇床
    {
        if (pcolor[0] & 0x7FFF)
        {
            if (!oneside)
            {
                return FALSE;
            }

            if ((e1[0] - s4) < rocknum ||
                (e4 - s1[1]) < rocknum)
            {
                return FALSE;
            }
        }
    }

    if (n != 2)
    {
        return FALSE;
    }

    if (s1[0] < 0 || s1[1] < 0 || s0 < 0 || s2 < 0)
    {
        return FALSE;
    }

    if (s0 < s2)
    {
        if (s1[1] - e0 < safe_len)
        {
            return FALSE;
        }

        if (s1[1] - e1[0] < safe_len)
        {
            return FALSE;
        }

        if (s2 - e1[0] < safe_len)
        {
            return FALSE;
        }
    }
    else
    {
        if (s0 - e1[0] < safe_len)
        {
            return FALSE;
        }

        if (s1[1] - e1[0] < safe_len)
        {
            return FALSE;
        }

        if (s1[1] - e2 < safe_len)
        {
            return FALSE;
        }

    }
    start[0] = s1[0];
    end[0] = e1[0];

    start[1] = s1[1];
    end[1] = e1[1];

    range0[0] = s0;
    range0[1] = e0;
    range2[0] = s2;
    range2[1] = e2;

    return TRUE;
}
/*
 *
 */
void compress_free (void)
{

}
/*
 * @row:运行行数，从0开始
 * @threhold[out]:阈值和限速的信息
 * @ret:NONE
 */
void compress_get_threhold(MH_KNIT *p, int row, RUN_THREHOLD *run_threhold)
{
    if (row < 0 || row >= p->total_line)
    {
        run_threhold->threhold = 0;
        run_threhold->limit_speed = 0;
    }
    else if(!p->ctrl_page)
    {
        run_threhold->threhold = 0;
        run_threhold->limit_speed = 0;
    }
    else
    {
        run_threhold->threhold = p->ctrl_page[row].threhold;
        run_threhold->limit_speed = p->ctrl_page[row].limit_speed;
    }
}
/*
 * @pdes:当前行
 * @psrc:被合并的行
 */
void compress_row (KNIT_PAGE *pdes, KNIT_PAGE *psrc)
{
    pdes->flw_3F = psrc->flw_1F;
    pdes->act_3FA = psrc->act_1FA;
    pdes->code_3FA = psrc->code_1FA;
    pdes->act_3FH = psrc->act_1FH;
    pdes->code_3FH = psrc->code_1FH;

    pdes->flw_3B = psrc->flw_1B;
    pdes->act_3BA = psrc->act_1BA;
    pdes->code_3BA = psrc->code_1BA;
    pdes->act_3BH = psrc->act_1BH;
    pdes->code_3BH = psrc->code_1BH;

    pdes->rock2_dir.flag = psrc->rock.flag;
    pdes->rock2_dir.shake = psrc->rock.shake;
    pdes->rock2_dir.drt = psrc->rock.drt;
    pdes->rock2_num.needle = psrc->rock.needle;
    pdes->rock2_num.speed = psrc->rock.speed;
}
/*
 * 设置阈值与限速
 * @translator: 转换器
 * @psrc:控制行信息
 * @des_row:压缩之后的行数
 * NOTE:该函数用于读取压缩之后的色码范围
 */
int compress_set_threhold(MH_KNIT *p, CTRL_PAGE *psrc, int des_row)
{
    int n = des_row;
    int prow[2];
    int pcolor[2];
    int start;
    int end;

    KNIT_PAGE *pcon = psrc->pcon;

    prow[0] = pcon->flw_1F;
    prow[1] = pcon->flw_1B;
    pcolor[0] = pcon->code_1FA | pcon->code_1FH;
    pcolor[1] = pcon->code_1BA | pcon->code_1BH;
    translator_get_color_range (p->flower_data, p->flower_width,
            p->flower_height, prow, pcolor, &start, &end);

    psrc->threhold = (n % 2) ? end : start;
    psrc->limit_speed = 90;

    /* check system 3, 4 */
    prow[0] = pcon->flw_3F;
    prow[1] = pcon->flw_3B;
    pcolor[0] = pcon->code_3FA | pcon->code_3FH;
    pcolor[1] = pcon->code_3BA | pcon->code_3BH;
    translator_get_color_range(p->flower_data, p->flower_width,
            p->flower_height, prow, pcolor, &start, &end);

    if (start < 0 || end < 0)
    {
        return FALSE;
    }

    return TRUE;
}
/*
 * 设置阈值与限速
 * @translator: 转换器
 * @psrc:控制行信息
 * @des_row:压缩之后的行数(1开始)
 * NOTE:该函数用于读取压缩之后的色码范围
 */
int compress_set_2threhold (MH_KNIT *p, CTRL_PAGE *pctrl, int des_row)
{
    int n = des_row;
    int prow[2];
    int pcolor[2];
    int s[2];
    int e[2];
    int start;
    int end;

    KNIT_PAGE *pcon = pctrl->pcon;

    prow[0] = pcon->flw_1F;
    prow[1] = pcon->flw_1B;
    pcolor[0] = pcon->code_1FA | pcon->code_1FH;
    pcolor[1] = pcon->code_1BA | pcon->code_1BH;
    translator_get_color_range (p->flower_data, p->flower_width,
            p->flower_height, prow, pcolor, &s[0], &e[0]);

    prow[0] = pcon->flw_2F;
    prow[1] = pcon->flw_2B;
    pcolor[0] = pcon->code_2FA | pcon->code_2FH;
    pcolor[1] = pcon->code_2BA | pcon->code_2BH;
    translator_get_color_range (p->flower_data, p->flower_width,
            p->flower_height, prow, pcolor, &s[1], &e[1]);

    get_range (&start, &end, s, e);

    pctrl->threhold = (n % 2) ? end : start;
    pctrl->limit_speed = 90;

    /* check system 3, 4 */
    prow[0] = pcon->flw_3F;
    prow[1] = pcon->flw_3B;
    pcolor[0] = pcon->code_3FA | pcon->code_3FH;
    pcolor[1] = pcon->code_3BA | pcon->code_3BH;
    translator_get_color_range (p->flower_data, p->flower_width,
            p->flower_height, prow, pcolor, &s[0], &e[0]);

    prow[0] = pcon->flw_4F;
    prow[1] = pcon->flw_4B;
    pcolor[0] = pcon->code_4FA | pcon->code_4FH;
    pcolor[1] = pcon->code_4BA | pcon->code_4BH;
    translator_get_color_range (p->flower_data, p->flower_width,
            p->flower_height, prow, pcolor, &s[1], &e[1]);

    get_range (&start, &end, s, e);

    if (start < 0 || end < 0)
    {
        return FALSE;
    }

    return TRUE;
}
/*
 *
 */
static ushort GetDesLineAfterCompress(CTRL_PAGE *pctrl, ushort total, ushort src_line)
{
    int i = 0;
    int ret = 0;
    for(i = 0; i < total; ++i)
    {
        if(pctrl[i].src == src_line)
        {
            ret = i + 1;
            break;
        }
    }
    return ret;
}
/*
 * 节约开始行至多只能出现在被压缩行的第1行
 * 节约结束行至多只能出现在被压缩行的最后1行
 */
static void CheckEconomyForCompress(KNIT_PAGE *pcon, ushort total, ushort economy[][3], uint size)
{
    int i = 0, j = 0;
    for(i = 0; i < total; ++i)
    {
        ushort start = pcon[i].economy_start;
        ushort end = pcon[i].economy_end;
        ushort num = pcon[i].economy_num;
        if(start && end && num)
        {
            pcon[start - 1].end.es = TRUE;
            pcon[end - 1].end.ee = TRUE;
            if(end - start == 1)
            {
                pcon[start - 1].end.forbid = TRUE;
                pcon[end - 1].end.forbid = TRUE;
            }
            if(j < size)
            {
                economy[j][0] = start;
                economy[j][1] = end;
                economy[j][2] = num;
                ++j;
            }
            else
            {
                printf("Warnning: %s: %d\n", __FILE__, __LINE__);
            }
        }
    }
}
/*
 *
 */
static void DistributeEconomyAfterCompress(CTRL_PAGE *pctrl, ushort total, ushort economy[][3], uint size)
{
    int i = 0;
    for(i = 0; i < size; ++i)
    {
        if(economy[i][0])
        {
            ushort start = GetDesLineAfterCompress(pctrl, total, economy[i][0]);
            ushort end = GetDesLineAfterCompress(pctrl, total, economy[i][1]);
            if(start && end && ((end - start) & 1))
            {
                pctrl[end - 1].pcon->economy_start = start;
                pctrl[end - 1].pcon->economy_end = end;
                pctrl[end - 1].pcon->economy_num = economy[i][2];
            }
            else
            {
                printf("Warning: %s: %d(%d, %d)\n", __FILE__, __LINE__, economy[i][0], economy[i][1]);
            }
        }
    }
}
/** brief: 单系统直选机器合并
 * @program_info: 程式缓冲区
 * @total_rows: 要执行的总行数
 * @pat_fn:花样文件名
 * @ret: 压缩后的行数，失败返回0
 */
int compress_con(MH_KNIT *p)
{
    int i = 0;
    int n = 0;
    short threhold = 0;
    int ret1 = 0;
    int ret2 = 0;
    int rocktype = 0;
    short limit_speed = 0;
    int mactype = (int)(p->inch_type + 0.5);

    if (rocktype > 1)
    {
        return 0;
    }

    if (mactype < 12)
    {
        return 0;
    }

    if (!p->knit_page)
    {
        printf ("program info is null\n");
        return 0;
    }

    /*判断节约*/
    ushort economy[ECONOMY_MAX_NUM][3] = {{0}};
    CheckEconomyForCompress(p->knit_page, p->total_line, economy, ECONOMY_MAX_NUM);

    KNIT_PAGE *pcompress_prg = calloc (p->total_line, sizeof(KNIT_PAGE));
    CTRL_PAGE *pctrl_page = calloc(p->total_line, sizeof(CTRL_PAGE));
    for (i = 0; i < p->total_line; ++i)
    {
        KNIT_PAGE *psrc = &p->knit_page[i];
        KNIT_PAGE *pdes = pcompress_prg + n;
        CTRL_PAGE *pctrl = pctrl_page + n;
        pctrl->pcon = pdes;
        short threhold2 = 0;
        short limit_speed2 = 0;
        int type = 0;

        if(i + 2 < p->total_line)
        {
            ret1 = can_compress_ss(p, psrc, i + 1, n + 1, p->total_line, &threhold, &limit_speed);
        }
        if(ret1 && !psrc[1].end.ee && !psrc[2].end.es)
        {
            /* 判断3,4行也可以合并 */
            ret2 = can_compress_ss(p, psrc + 2, i + 3, n + 2,
                p->total_line, &threhold2, &limit_speed2);

            /*判断第3行是否空行 */
            if(ret2)
            {
                type = 4;
            }
            else
            {
                if(compress_is_null_row(psrc + 2))
                {
                    type = 3;
                }
            }
        }

        if (ret1 && (type == 4))
        {
            /* 4行合并成2行 */
            /* 合并1，2行 */
            if(ret1 == 1)
            {
                memcpy((void*)pdes, (void*)psrc, sizeof(KNIT_PAGE));
                compress_row(pdes, psrc + 1);
            }
            else if(ret1 == 2)
            {
                memcpy((void*)pdes, (void*)(psrc + 1), sizeof(KNIT_PAGE));
                compress_row(pdes, psrc);
            }

            pctrl->src = i + 1;

            pdes->economy_start = 0;
            pdes->economy_end = 0;
            pdes->economy_num = 0;

            pctrl->sub = 0;
            pctrl->row = n;
            pctrl->threhold = threhold;
            pctrl->limit_speed = limit_speed;
            i += 2;

            /* 合并3，4行 */
            n++;
            psrc = &p->knit_page[i];
            pdes = pcompress_prg + n;
            pctrl = pctrl_page + n;
            pctrl->pcon = pdes;

            if(ret2 == 1)
            {
                memcpy ((void*)pdes, (void*)psrc, sizeof (KNIT_PAGE));
                compress_row(pdes, psrc + 1);
            }
            else if(ret2 == 2)
            {
                memcpy ((void*)pdes, (void*)(psrc + 1), sizeof (KNIT_PAGE));
                compress_row(pdes, psrc);
            }

            pctrl->src = i + 2;

            pdes->economy_start = 0;
            pdes->economy_end = 0;
            pdes->economy_num = 0;

            pctrl->sub = 0;
            pctrl->row = n;
            pctrl->threhold = threhold2;
            pctrl->limit_speed = limit_speed2;

            i += 1;
        }
        else if (ret1 && (type == 3))
        {
            /* 3行合并成1行 */
            /* 合并1，2行,第3行为空行 */
            if(ret1 == 1)
            {
                memcpy((void*)pdes, (void*)psrc, sizeof (KNIT_PAGE));
                compress_row(pdes, psrc + 1);
            }
            else if(ret1 == 2)
            {
                memcpy((void*)pdes, (void*)(psrc + 1), sizeof(KNIT_PAGE));
                compress_row(pdes, psrc);
            }

            if(psrc[2].end.ee)
            {
                pctrl->src = i + 3;
            }
            else
            {
                pctrl->src = i + 1;
            }

            pctrl->sub = 0;
            pctrl->row = n;
            pctrl->threhold = threhold;
            pctrl->limit_speed = limit_speed;
            i += 2;
        }
        else
        {
            memcpy ((void*)pdes, (void*)psrc, sizeof (KNIT_PAGE));
            pctrl->src = i + 1;

            pctrl->sub = 0;
            pctrl->row = n;

            uchar *pdrt = (uchar*)&pdes->rock2_dir;
            uchar *pnum = (uchar*)&pdes->rock2_num;

            if ((*pdrt != 0) || (*pnum != 0))//已经压缩过的程式
            {
                if (!compress_set_threhold (p, pctrl, n + 1))
                {
                    *pdrt = 0;
                    *pnum = 0;
                    pctrl->threhold = 0;
                    pctrl->limit_speed = 0;
                }
            }
            else
            {
                pctrl->threhold = 0;
                pctrl->limit_speed = 0;
            }
        }
        n++;
    }

    printf ("compress pages = %d\n", n);
    /*节约重新分配*/
    DistributeEconomyAfterCompress(pctrl_page, n, economy, ECONOMY_MAX_NUM);

    FREE(p->knit_page_head);
    p->knit_page_head = calloc(n + 1, sizeof(KNIT_PAGE));
    p->knit_page = p->knit_page_head + 1;
    memcpy(p->knit_page, pcompress_prg, n * sizeof(KNIT_PAGE));
    FREE(p->ctrl_page);
    p->ctrl_page = calloc(n, sizeof(CTRL_PAGE));
    memcpy(p->ctrl_page, pctrl_page, n * sizeof(CTRL_PAGE));

    FREE(pctrl_page);
    FREE(pcompress_prg);

    return n;
}
/*
 *
 */
static int is_sys_null (int sys, KNIT_PAGE *pconline)
{
    if (sys == 1)
    {
        if ((pconline->act_1BA == 0) &&
            (pconline->act_1FA == 0) &&
            (pconline->code_1BA == 0) &&
            (pconline->code_1BH == 0) &&
            (pconline->code_1FA == 0) &&
            (pconline->code_1FH == 0))
        {
            return TRUE;
        }
    }
    else if (sys == 2)
    {
        if ((pconline->act_2BA == 0) &&
            (pconline->act_2FA == 0) &&
            (pconline->code_2BA == 0) &&
            (pconline->code_2BH == 0) &&
            (pconline->code_2FA == 0) &&
            (pconline->code_2FH == 0))
        {
            return TRUE;
        }
    }

    return FALSE;
}
/*
 *
 */
static void swap_sys1sys2 (uchar *pconline)
{
    uchar tmp[sizeof(KNIT_PAGE)] = {0};
    memcpy (tmp, pconline, sizeof (tmp));

    memcpy (pconline, tmp + 20, 20);
    memcpy (pconline + 20, tmp, 20);
}
/*
 * @pdes:当前行
 * @psrc:被合并的行
 */
void compress_2row (KNIT_PAGE *pdes, KNIT_PAGE *psrc)
{
    KNIT_PAGE tmp = { 0 };
    uchar *qdes = (uchar *) pdes;
    uchar *qsrc = (uchar *) psrc;

    memcpy ((void*)&tmp, psrc, sizeof (tmp));

    if (is_sys_null(1, pdes))
    {
        swap_sys1sys2 (qdes);
    }

    //色码合并，下一行的摇床填到113， 114，三角动作，色码，行数写到系统3，系统4上
    //第一次摇床和第二次摇床都使用系统1摇床
    if (is_sys_null (1, psrc))
    {
        swap_sys1sys2 ((uchar*)&tmp);
    }

    pdes->flw_3F = tmp.flw_1F;
    pdes->act_3FA = tmp.act_1FA;
    pdes->code_3FA = tmp.code_1FA;
    pdes->act_3FH = tmp.act_1FH;
    pdes->code_3FH = tmp.code_1FH;

    pdes->flw_3B = tmp.flw_1B;
    pdes->act_3BA = tmp.act_1BA;
    pdes->code_3BA = tmp.code_1BA;
    pdes->act_3BH = tmp.act_1BH;
    pdes->code_3BH = tmp.code_1BH;

    pdes->flw_4F = tmp.flw_2F;
    pdes->act_4FA = tmp.act_2FA;
    pdes->code_4FA = tmp.code_2FA;
    pdes->act_4FH = tmp.act_2FH;
    pdes->code_4FH = tmp.code_2FH;

    pdes->flw_4B = tmp.flw_2B;
    pdes->act_4BA = tmp.act_2BA;
    pdes->code_4BA = tmp.code_2BA;
    pdes->act_4BH = tmp.act_2BH;
    pdes->code_4BH = tmp.code_2BH;

    qdes[113] = qsrc[100];
    qdes[117] = qsrc[101];
}
/*
 * 不共摇床的收针
 * @pdes:压缩之后的缓冲区
 * @psrc:压缩之前的缓冲区
 * @des_row:压缩缓冲区当前的行数
 * @type: 1,机头从左边开始收针，按正常顺序
 *        2,机头从右边开始收针，需要打乱执行顺序，先收右边，再收左边
 * @threhold:阈值
 * @limit_speed:限速
*/
void compress_2rock (KNIT_PAGE *pdes,
                     KNIT_PAGE *psrc,
                     int type)
{
    if (type == 1)
    {
        memcpy ((void*)pdes, (void*)psrc, sizeof (KNIT_PAGE));
        /**< 合并 */
        compress_2row (pdes, psrc + 1);
    }
    else
    {
        /**< 交换当前行与下一行 */
        memcpy ((void*)pdes, (void*)(psrc + 1), sizeof (KNIT_PAGE));
        /**< 合并 */
        compress_2row (pdes, psrc);
    }
}
/*
 * @p[in/out]: 要转换的数据缓冲区
 * @translator:
 * @start:start[0]，左侧色码段的起点,start[1]，右侧色码段的起点
 * @end:end[0]，左侧色码段的终点,end[1]，右侧色码段的终点
 * @type:0,转换左侧色码；1，转换右侧色码
 * NOTE:制版系统解译出来的动作是一个系统翻两边的色码，
 *      这个函数将动作转换成，左侧的色码用1个系统翻，右侧的色码用一个系统翻
 */
static int transform_color_cmd (MH_KNIT *p,
                                KNIT_PAGE *pcon,
                                int *start,
                                int *end,
                                int type)
{
    int color = 0;
    int i = 0;
    int y = 0;
    char actF = 0x04;
    char actB = 0x01;
    int colorFA = 0;
    int colorFH = 0;
    int colorBA = 0;
    int colorBH = 0;

    y = pcon->flw_1F;
    if (y == 0)
    {
        y = pcon->flw_2F;
    }

    if (y <= 0)
    {
        return FALSE;
    }

    color = 0;
    for (i = start[type]; i <= end[type]; i++)
    {
        char c = translator_get_color(p->flower_data, p->flower_width, p->flower_height, i, y - 1);
        color |= (0x01 << c);
    }

    //system1
    if (pcon->code_1FA & color)
    {
        actF = 0x01;
        actB = 0x04;
    }

    colorBA = color;
    colorBH = 0;
    colorFA = 0;
    colorFH = color;

    if (actF == 0x01)
    {
        colorFA = color;
        colorFH = 0;
        colorBA = 0;
        colorBH = color;
    }

    pcon->act_1FA = actF;
    pcon->code_1FA = pcon->code_1FA & colorFA;
    pcon->act_1FH = actF;
    pcon->code_1FH = pcon->code_1FH & colorFH;
    if ((pcon->code_1FA == 0) &&
        (pcon->code_1FH == 0))
    {
        pcon->flw_1F = 0;
    }

    pcon->act_1BA = actB;
    pcon->code_1BA = pcon->code_1BA & colorBA;
    pcon->act_1BH = actB;
    pcon->code_1BH = pcon->code_1BH & colorBH;
    if ((pcon->code_1FA == 0) &&
        (pcon->code_1FH == 0))
    {
        pcon->flw_1B = 0;
    }

    //system2
    actF = 0x04;
    actB = 0x01;
    if (pcon->code_2FA & color)
    {
        actF = 0x01;
        actB = 0x04;
    }
    colorBA = color;
    colorBH = 0;
    colorFA = 0;
    colorFH = color;

    if (actF == 0x01)
    {
        colorFA = color;
        colorFH = 0;
        colorBA = 0;
        colorBH = color;
    }

    pcon->act_2FA = actF;
    pcon->code_2FA = pcon->code_2FA & colorFA;
    pcon->act_2FH = actF;
    pcon->code_2FH = pcon->code_2FH & colorFH;
    if ((pcon->code_2FA == 0) &&
        (pcon->code_2FH == 0))
    {
        pcon->flw_2F = 0;
    }

    pcon->act_2BA = actB;
    pcon->code_2BA = pcon->code_2BA & colorBA;
    pcon->act_2BH = actB;
    pcon->code_2BH = pcon->code_2BH & colorBH;
    if ((pcon->code_2BA == 0) &&
        (pcon->code_2BH == 0))
    {
        pcon->flw_2F = 0;
    }

    return TRUE;
}
/*
 * 不共摇床的收针
 * @pdes:压缩之后的缓冲区
 * @psrc:压缩之前的缓冲区
 * @translator:转换器
 * @des_row:压缩缓冲区当前的行数
 * @range0:第一行范围
 * @range2:第三行范围
 * @start:第二行起点，0，左侧收针起点，1，右侧收针起点
 * @end:第二行终点，0，左侧收针终点，1，右侧收针终点
*/
void compress_same_rock (MH_KNIT *p,
                         KNIT_PAGE *pdes,
                         KNIT_PAGE *psrc,
                         CTRL_PAGE *pctrl,
                         int *des_row,
                         int src_row,
                         int *range0,
                         int *range2,
                         int *start,
                         int *end)
{
    KNIT_PAGE tmp[4] = { { 0 } };
    //先分成4行，再压缩
    memcpy ((void*)&tmp[0], (void*)(psrc + 0), sizeof (KNIT_PAGE)); //tmp[0], 第一行收针
    memcpy ((void*)&tmp[1], (void*)(psrc + 1), sizeof (KNIT_PAGE)); //tmp[1], 第二行左侧收针
    memcpy ((void*)&tmp[2], (void*)(psrc + 1), sizeof (KNIT_PAGE)); //tmp[2], 第二行右侧收针
    memcpy ((void*)&tmp[3], (void*)(psrc + 2), sizeof (KNIT_PAGE)); //tmp[3], 第三行收针

    transform_color_cmd (p, &tmp[1], start, end, 0);
    transform_color_cmd (p, &tmp[2], start, end, 1);

    /* 从左侧开始收针,前编织的收针 */
    /*
                      -----(3)
       -----(1)       -----(2)
       -----(0)
    */
    if (range0[0] < start[1])
    {
        if ((*des_row + 1) & 1)      //rightward
        {
            //按照0, 2, 3, 1的顺序执行
            memcpy ((void*)pdes, (void*)&tmp[0], sizeof (KNIT_PAGE));
            /**< 合并 */
            compress_2row (pdes, &tmp[2]);

            pctrl[0].src = src_row;

            pdes[0].economy_start = 0;
            pdes[0].economy_end = 0;
            pdes[0].economy_num = 0;
            //第一行与第二行右侧收针
            pctrl[0].threhold = range0[1];
            pctrl[0].limit_speed = 90;
            pctrl[0].sub = 0;
            pctrl[0].row = *des_row;
            (*des_row)++;
            memcpy ((void*)(pdes + 1), (void*)&tmp[3], sizeof (KNIT_PAGE));
            /**< 合并 */
            compress_2row (pdes + 1, &tmp[1]);

            pctrl[1].src = src_row + 1;

            pdes[1].economy_start = 0;
            pdes[1].economy_end = 0;
            pdes[1].economy_num = 0;
            //第三行与第二行左侧收针
            pctrl[1].threhold = range2[0];
            pctrl[1].limit_speed = 90;
            pctrl[1].sub = 0;
            pctrl[1].row = *des_row;
        }
        else            //leftward
        {
            //按照2, 0, 1, 3的顺序执行
            memcpy ((void*)pdes, (void*)&tmp[2], sizeof (KNIT_PAGE));
            /**< 合并 */
            compress_2row (pdes, &tmp[0]);

            pctrl[0].src = src_row;

            pdes[0].economy_start = 0;
            pdes[0].economy_end = 0;
            pdes[0].economy_num = 0;
            //第二行右侧收针与第一行
            pctrl[0].sub = 0;
            pctrl[0].row = *des_row;
            pctrl[0].threhold = start[1];
            pctrl[0].limit_speed = 90;
            (*des_row)++;
            memcpy ((void*)(pdes + 1), (void*)&tmp[1], sizeof (KNIT_PAGE));
            /**< 合并 */
            compress_2row (pdes + 1, &tmp[3]);

            pctrl[1].src = src_row + 1;

            pdes[1].economy_start = 0;
            pdes[1].economy_end = 0;
            pdes[1].economy_num = 0;
            //第二行左侧收针与第三行
            pctrl[1].sub = 0;
            pctrl[1].row = *des_row;
            pctrl[1].threhold = end[0];
            pctrl[1].limit_speed = 90;
        }
    }
    else
    {
        /*
            -----(3)
            -----(1)       -----(2)
                           -----(0)
        */
        if ((*des_row + 1) & 1)      //rightward
        {
            //按照1, 0, 2, 3的顺序执行
            memcpy ((void*)pdes, (void*)&tmp[1], sizeof (KNIT_PAGE));
            /**< 合并 */
            compress_2row (pdes, &tmp[0]);

            pctrl[0].src = src_row;
            pdes[0].economy_start = 0;
            pdes[0].economy_end = 0;
            pdes[0].economy_num = 0;
            //第一行与第二行右侧收针
            pctrl[0].threhold = end[0];
            pctrl[0].limit_speed = 90;
            pctrl[0].sub = 0;
            pctrl[0].row = *des_row;
            (*des_row)++;
            memcpy ((void*)(pdes + 1), (void*)&tmp[2], sizeof (KNIT_PAGE));
            /**< 合并 */
            compress_2row (pdes + 1, &tmp[3]);

            pctrl[1].src = src_row + 2;
            pdes[1].economy_start = 0;
            pdes[1].economy_end = 0;
            pdes[1].economy_num = 0;
            //第三行与第二行左侧收针
            pctrl[1].threhold = start[1];
            pctrl[1].limit_speed = 90;
            pctrl[1].sub = 0;
            pctrl[1].row = *des_row;
        }
        else            //leftward
        {
            //按照0, 1, 3, 2的顺序执行
            memcpy ((void*)pdes, (void*)&tmp[0], sizeof (KNIT_PAGE));
            /**< 合并 */
            compress_2row (pdes, &tmp[1]);

            pctrl[0].src = src_row;
            pdes[0].economy_start = 0;
            pdes[0].economy_end = 0;
            pdes[0].economy_num = 0;
            //第二行右侧收针与第一行
            pctrl[0].sub = 0;
            pctrl[0].row = *des_row;
            pctrl[0].threhold = range0[0];
            pctrl[0].limit_speed = 90;
            (*des_row)++;
            memcpy ((void*)(pdes + 1), (void*)&tmp[3], sizeof (KNIT_PAGE));
            /**< 合并 */
            compress_2row (pdes + 1, &tmp[2]);

            pctrl[1].src = src_row + 2;
            pdes[1].economy_start = 0;
            pdes[1].economy_end = 0;
            pdes[1].economy_num = 0;
            //第二行左侧收针与第三行
            pctrl[1].sub = 0;
            pctrl[1].row = *des_row;
            pctrl[1].threhold = range2[1];
            pctrl[1].limit_speed = 90;
        }
    }
}
/*
 * brief: 双系统压缩
 * @program_info: 程式缓冲区
 * @total_rows: 要执行的总行数
 * @pat_fn:花样文件名
 * @ret: 压缩后的行数，失败返回0
 */
int compress_cnt(MH_KNIT *p)
{
    int i = 0;
    int n = 0;
    short threhold = 0;
    int rocktype = 0;
    short limit_speed = 0;
    int range0[2] = { 0 };
    int range2[2] = { 0 };
    int start[2] = { 0 };
    int end[2] = { 0 };
    int mactype = (int)(p->inch_type + 0.5);

    if (rocktype > 1)
    {
        return FALSE;
    }

    if (mactype < 12)
    {
        return FALSE;
    }

    if (!p->knit_page)
    {
        printf ("program info is null\n");
        return FALSE;
    }

    /*判断节约*/
    ushort economy[ECONOMY_MAX_NUM][3] = { { 0 } };
    CheckEconomyForCompress(p->knit_page, p->total_line, economy, ECONOMY_MAX_NUM);

    KNIT_PAGE *pcompress_prg = calloc (p->total_line, sizeof(KNIT_PAGE));
    CTRL_PAGE *pctrl_page = calloc(p->total_line, sizeof(CTRL_PAGE));

    for (i = 0; i < p->total_line; ++i)
    {
        KNIT_PAGE *psrc = &p->knit_page[i];
        KNIT_PAGE *pdes = pcompress_prg + n;
        CTRL_PAGE *pctrl = pctrl_page + n;
        pctrl->pcon = pdes;

        int ret = 0;
        if(i + 2 < p->total_line)
        {
            ret = can_compress(p, psrc, i + 1, n + 1, p->total_line, &threhold, &limit_speed);
        }

        if(ret) //不同摇床的情况
        {
            compress_2rock(pdes, psrc, ret);
            pctrl->src = i + 1;

            pdes->economy_start = 0;
            pdes->economy_end = 0;
            pdes->economy_num = 0;

            pctrl->sub = 0;
            pctrl->row = n;
            pctrl->threhold = threhold;
            pctrl->limit_speed = limit_speed;
            i += 2;
        }
        else if(can_compress2(p, psrc, i + 1, p->total_line, &limit_speed,
                range0, range2, start, end)) //共摇床的情况
        {
            compress_same_rock(p, pdes, psrc, pctrl, &n, i + 1,
                    range0, range2, start, end);
            i += 3;
        }
        else
        {
            memcpy((void*) pdes, (void*) psrc, sizeof(KNIT_PAGE));
            pctrl->src = i + 1;
            pctrl->sub = 0;
            pctrl->row = n;

            uchar *pdrt = (uchar*)&pdes->rock2_dir;
            uchar *pnum = (uchar*)&pdes->rock2_num;

            if ((*pdrt != 0) || (*pnum != 0))//已经压缩过的程式
            {
                if(!compress_set_2threhold(p, pctrl, n + 1))
                {
                    *pdrt = 0;
                    *pnum = 0;
                    pctrl->threhold = 0;
                    pctrl->limit_speed = 0;
                }
            }
            else
            {
                pctrl->threhold = 0;
                pctrl->limit_speed = 0;
            }
        }
        n++;
    }

    /*节约重新分配*/
    DistributeEconomyAfterCompress(pctrl_page, n, economy, ECONOMY_MAX_NUM);

    FREE(p->knit_page_head);
    p->knit_page_head = calloc(n + 1, sizeof(KNIT_PAGE));
    p->knit_page = p->knit_page_head + 1;
    memcpy(p->knit_page, pcompress_prg, n * sizeof(KNIT_PAGE));
    FREE(p->ctrl_page);
    p->ctrl_page = calloc(n, sizeof(CTRL_PAGE));
    memcpy(p->ctrl_page, pctrl_page, n * sizeof(CTRL_PAGE));

    FREE(pctrl_page);
    FREE(pcompress_prg);


    return n;
}
/** \brief  :压缩执行的程式
 *
 * \param
 * \param
 * \return
 * \date    :2014\05\24
 * \author  :herbert
 */
void CompressRockCtrlPage(MH_KNIT *p)
{
    int rows = 0;
    if (!p->knit_page)
    {
        return;
    }
    printf ("compress start\n");
    //这里要使用end_line_bak成员，否则压缩两次就会造成结果不一致,因为end_line在压缩之后就会变化
    if(p->sys_info.cam == 2)
    {
        rows = compress_cnt(p);
    }
    else
    {
        rows = compress_con(p);
    }
    printf ("rows after compress = %d\n", rows);
    if (rows > 0)
    {
        p->total_line = rows;
    }
    printf ("compress end\n");
}
/*
 *
 */
int GetRockCompressEnable(MH_KNIT *p)
{
    return p->sys_info.rock_compress;
}
/*
 *
 */
void SetRockCompressEnable(MH_KNIT *p, int enable)
{
    p->sys_info.rock_compress = enable;
}
